package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.VO.PageInfoResult;
import com.student.VO.SnackVO;
import com.student.dto.ResponseDto;
import com.student.mapper.SnackOrderMapper;
import com.student.pojo.SnackGoods;
import com.student.pojo.SnackOrder;
import com.student.pojo.User;
import com.student.service.SnackOrderService;
import com.student.util.PageResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: makeups
 * @BelongsPackage: com.student.service.impl
 * @Author: 小徐
 * @CreateTime: 2024-12-21 20:05
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class SnackOrderServiceImpl implements SnackOrderService {
    @Autowired
    private SnackOrderMapper snackOrderMapper;

    @Override
    public ResponseDto getAllOrderPerson(SnackOrder snackOrder) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(snackOrder.getCurrentPage(), snackOrder.getPageSize());
        List<SnackOrder> phAdminDO = snackOrderMapper.getAllOrderPerson(snackOrder);

        PageInfo<SnackOrder> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto addTaking(Integer id, String taking) {
        Integer res = snackOrderMapper.addTaking(id, taking);
        if (res > 0) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }


    @Override
    public ResponseDto getAllOrder(SnackOrder snackOrder) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(snackOrder.getCurrentPage(), snackOrder.getPageSize());
        List<SnackOrder> phAdminDO = snackOrderMapper.getAllOrder(snackOrder);

        PageInfo<SnackOrder> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto updateStatics(Integer status, Integer id, Integer adminId) {
        Integer res = snackOrderMapper.updateStatics(status, id, adminId);
        if (res >= 1) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteOrder(Integer id) {
        Integer res = snackOrderMapper.deleteOrder(id);
        if (res >= 1) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addTatolMoney(Integer id, double tatol, String address, String phone, String name) {
        SnackOrder snackOrder = new SnackOrder();
        snackOrder.setUserId(id);
        snackOrder.setOrderTotal(tatol);
        snackOrder.setAddress(address);
        snackOrder.setPhone(phone);
        snackOrder.setName(name);
        User user = snackOrderMapper.selectMoney(tatol, id);
        if (user == null) {
            return new ResponseDto(2, "亲，你的余额不足", null);
        }
        Integer res = snackOrderMapper.addOrderPerson(snackOrder);
        return ResponseDto.success(snackOrder.getId());
    }


    @Override
    public ResponseDto updateStaticsUser(Integer id, Integer status) {
        Integer res = snackOrderMapper.updateStaticsUser(id, status);
        if (res >= 1) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto getAllOrderMannger(SnackOrder snackOrder) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(snackOrder.getCurrentPage(), snackOrder.getPageSize());
        List<SnackOrder> phAdminDO = snackOrderMapper.getAllOrderMannger(snackOrder);

        PageInfo<SnackOrder> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto addOrderPerson(Integer id, double tatol, String address, String phone, String name, double price, Integer proID, Integer num) {
        double prices = price * num;
        SnackOrder snackOrder = new SnackOrder();
        snackOrder.setUserId(id);
        snackOrder.setOrderTotal(tatol);
        snackOrder.setAddress(address);
        snackOrder.setPhone(phone);
        snackOrder.setName(name);
        User user = snackOrderMapper.selectMoney(tatol, id);
        if (user == null) {
            return new ResponseDto(2, "亲，你的余额不足", null);
        } else {
//            查询库存是否还有
            SnackGoods snackGoods = snackOrderMapper.selectProductNum(proID, num);
            if (snackGoods != null) {
                //            添加订单信息
                Integer res = snackOrderMapper.addOrderPerson(snackOrder);
//                这边做库存减少
                Integer res1 = snackOrderMapper.updateProduct(proID, num);
                System.err.println("插入订单id：" + snackOrder.getId());
                if (res >= 0) {
//                这边是用户资金减少
                    Integer userUpdate = snackOrderMapper.userUpdateMoney(tatol, id);
                    Integer month = snackOrderMapper.goodNumber(proID, num);

                    return ResponseDto.success(null);
                }
            } else {
                return new ResponseDto(2, "亲没有库存了", null);
            }

        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addOrderPersonTotal(Integer id, double tatol, String address, String phone, String name, double price, Integer proID, Integer num, Integer number, Integer cardIds, Integer orderID) {
        double prices = price * number;

        SnackGoods snackGoods = snackOrderMapper.selectProductNum(proID, number);
        if (snackGoods != null) {

            //                这边做库存减少
            Integer res1 = snackOrderMapper.updateProduct(proID, number);

            if (res1 >= 0) {
                Integer month = snackOrderMapper.goodNumber(proID, number);
//                这边是用户资金减少
                Integer userUpdate = snackOrderMapper.userUpdateMoney(tatol, id);
                Integer delete = snackOrderMapper.deleteSnackCard(cardIds);
                return ResponseDto.success(null);
            }
        } else {
            return new ResponseDto(2, "亲没有库存了", null);
        }


        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteCard(Integer id) {
        Integer delete = snackOrderMapper.deleteSnackCard(id);
        if (delete > 0) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }


}